package com.weixin.IOStream;

import org.junit.Test;

import java.io.*;

/**
 * @ClassName WebCoding
 * @Description 网络编程
 * 1.ObjectInputStream\ObjectOutputStream   序列化与非序列化
 * @date 2022/2/17 13:29
 * @author: wx
 * @Version 1.0
 */
public class WebCoding {

    @Test
    public void ObjectStreamTest() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out.data"));
        oos.writeObject(new String("我爱北京天安门"));
        oos.flush();
        oos.close();
    }

    @Test
    public void ObjectStreamOut() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out.data"));
        Object o = ois.readObject();
        String str = (String) o;
        System.out.println(str);
        ois.close();
    }

    @Test
    public void randomAccessFileTest() throws IOException {
        RandomAccessFile srcFile = new RandomAccessFile(new File("a.png"), "r");
        RandomAccessFile disFile = new RandomAccessFile(new File("n.png"), "rw");

        byte[] bytes = new byte[10];
        int len;
        if ((len = srcFile.read(bytes)) != -1) {
//            disFile.seek(5);
//            disFile.write(1001111);
            disFile.write(bytes, 0, len);
        }
        srcFile.close();
        disFile.close();
    }
}
